/*     */ package com.zimbra.cs.util;
/*     */ 
/*     */ import com.zimbra.common.mime.shim.JavaMailInternetAddress;
/*     */ import com.zimbra.common.net.SocketFactories;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.common.zmime.ZMimeMessage;
/*     */ import com.zimbra.common.zmime.ZSharedFileInputStream;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.util.Arrays;
/*     */ import java.util.Properties;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.Session;
/*     */ import javax.mail.Transport;
/*     */ import javax.mail.internet.InternetAddress;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.CommandLineParser;
/*     */ import org.apache.commons.cli.GnuParser;
/*     */ import org.apache.commons.cli.HelpFormatter;
/*     */ import org.apache.commons.cli.Option;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SmtpInject
/*     */ {
/*  53 */   private static Log mLog = LogFactory.getLog(SmtpInject.class);
/*     */   
/*  55 */   private static Options mOptions = new Options();
/*     */   
/*     */   static {
/*  58 */     mOptions.addOption("h", "help", false, "show help text");
/*  59 */     mOptions.addOption("f", "file", true, "rfc822/MIME formatted text file");
/*  60 */     mOptions.addOption("a", "address", true, "smtp server (default localhost)");
/*  61 */     mOptions.addOption("s", "sender", true, "envelope sender (mail from)");
/*  62 */     Option ropt = new Option("r", "recipient", true, "envelope recipients (rcpt to)");
/*  63 */     ropt.setArgs(-2);
/*  64 */     mOptions.addOption(ropt);
/*  65 */     mOptions.addOption("T", "trace", false, "trace server/client traffic");
/*  66 */     mOptions.addOption("t", "tls", false, "use TLS");
/*  67 */     mOptions.addOption("A", "auth", false, "use SMTP auth");
/*  68 */     mOptions.addOption("u", "username", true, "username for SMTP auth");
/*  69 */     mOptions.addOption("p", "password", true, "password for SMTP auth");
/*  70 */     mOptions.addOption("v", "verbose", false, "show provided options");
/*     */   }
/*     */   
/*     */   private static void usage(String errmsg) {
/*  74 */     if (errmsg != null) {
/*  75 */       mLog.error(errmsg);
/*     */     }
/*  77 */     HelpFormatter formatter = new HelpFormatter();
/*  78 */     formatter.printHelp("SmtpInject [options]", mOptions);
/*  79 */     System.exit(errmsg == null ? 0 : 1);
/*     */   }
/*     */   
/*     */   private static CommandLine parseArgs(String[] args) {
/*  83 */     StringBuffer gotCL = new StringBuffer("cmdline: ");
/*  84 */     for (int i = 0; i < args.length; i++) {
/*  85 */       gotCL.append("'").append(args[i]).append("' ");
/*     */     }
/*     */     
/*     */ 
/*  89 */     CommandLineParser parser = new GnuParser();
/*  90 */     CommandLine cl = null;
/*     */     try {
/*  92 */       cl = parser.parse(mOptions, args);
/*     */     } catch (ParseException pe) {
/*  94 */       usage(pe.getMessage());
/*     */     }
/*  96 */     return cl;
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/* 100 */     CliUtil.toolSetup();
/* 101 */     CommandLine cl = parseArgs(args);
/*     */     
/* 103 */     if (cl.hasOption("h")) {
/* 104 */       usage(null);
/*     */     }
/*     */     
/* 107 */     String file = null;
/* 108 */     if (!cl.hasOption("f")) {
/* 109 */       usage("no file specified");
/*     */     } else {
/* 111 */       file = cl.getOptionValue("f");
/*     */     }
/*     */     try {
/* 114 */       ByteUtil.getContent(new File(file));
/*     */     } catch (IOException ioe) {
/* 116 */       usage(ioe.getMessage());
/*     */     }
/*     */     
/* 119 */     String host = null;
/* 120 */     if (!cl.hasOption("a")) {
/* 121 */       usage("no smtp server specified");
/*     */     } else {
/* 123 */       host = cl.getOptionValue("a");
/*     */     }
/*     */     
/* 126 */     String sender = null;
/* 127 */     if (!cl.hasOption("s")) {
/* 128 */       usage("no sender specified");
/*     */     } else {
/* 130 */       sender = cl.getOptionValue("s");
/*     */     }
/*     */     
/* 133 */     String recipient = null;
/* 134 */     if (!cl.hasOption("r")) {
/* 135 */       usage("no recipient specified");
/*     */     } else {
/* 137 */       recipient = cl.getOptionValue("r");
/*     */     }
/*     */     
/* 140 */     boolean trace = false;
/* 141 */     if (cl.hasOption("T")) {
/* 142 */       trace = true;
/*     */     }
/*     */     
/* 145 */     boolean tls = false;
/* 146 */     if (cl.hasOption("t")) {
/* 147 */       tls = true;
/*     */     }
/*     */     
/* 150 */     boolean auth = false;
/* 151 */     String user = null;
/* 152 */     String password = null;
/* 153 */     if (cl.hasOption("A")) {
/* 154 */       auth = true;
/* 155 */       if (!cl.hasOption("u")) {
/* 156 */         usage("auth enabled, no user specified");
/*     */       } else {
/* 158 */         user = cl.getOptionValue("u");
/*     */       }
/* 160 */       if (!cl.hasOption("p")) {
/* 161 */         usage("auth enabled, no password specified");
/*     */       } else {
/* 163 */         password = cl.getOptionValue("p");
/*     */       }
/*     */     }
/*     */     
/* 167 */     if (cl.hasOption("v")) {
/* 168 */       mLog.info("SMTP server: " + host);
/* 169 */       mLog.info("Sender: " + sender);
/* 170 */       mLog.info("Recipient: " + recipient);
/* 171 */       mLog.info("File: " + file);
/* 172 */       mLog.info("TLS: " + tls);
/* 173 */       mLog.info("Auth: " + auth);
/* 174 */       if (auth) {
/* 175 */         mLog.info("User: " + user);
/* 176 */         char[] dummyPassword = new char[password.length()];
/* 177 */         Arrays.fill(dummyPassword, '*');
/* 178 */         mLog.info("Password: " + new String(dummyPassword));
/*     */       }
/*     */     }
/*     */     
/* 182 */     Properties props = System.getProperties();
/*     */     
/* 184 */     props.put("mail.smtp.host", host);
/*     */     
/* 186 */     if (auth) {
/* 187 */       props.put("mail.smtp.auth", "true");
/*     */     } else {
/* 189 */       props.put("mail.smtp.auth", "false");
/*     */     }
/*     */     
/* 192 */     if (tls) {
/* 193 */       props.put("mail.smtp.starttls.enable", "true");
/*     */     } else {
/* 195 */       props.put("mail.smtp.starttls.enable", "false");
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 200 */     props.put("mail.smtp.ssl.socketFactory", SocketFactories.dummySSLSocketFactory());
/*     */     
/* 202 */     Session session = Session.getInstance(props, null);
/* 203 */     session.setDebug(trace);
/*     */     
/*     */     try
/*     */     {
/* 207 */       MimeMessage msg = new ZMimeMessage(session, new ZSharedFileInputStream(file));
/* 208 */       InternetAddress[] address = { new JavaMailInternetAddress(recipient) };
/* 209 */       msg.setFrom(new JavaMailInternetAddress(sender));
/*     */       
/*     */ 
/* 212 */       Transport transport = session.getTransport("smtp");
/* 213 */       transport.connect(null, user, password);
/* 214 */       transport.sendMessage(msg, address);
/*     */     }
/*     */     catch (MessagingException mex) {
/* 217 */       mex.printStackTrace();
/* 218 */       Exception ex = null;
/* 219 */       if ((ex = mex.getNextException()) != null) {
/* 220 */         ex.printStackTrace();
/*     */       }
/* 222 */       System.exit(1);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/SmtpInject.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */